﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Extensil.Common;
using Extensil;
using System.Reflection;
using System.IO;
using Extensil.Processor;
using TaskAutomation.General;
//using Extensil.Common.General;

namespace TaskAutomation
{
    public class TaskReader
    {
        string _taskLog;
        string TaskLogFormat
        {
            get
            {
                if(String.IsNullOrEmpty(_taskLog))
                {
                    var config = TaskSettings.GetConfig();
                    _taskLog = config.TaskLogFormat;
                }
                return _taskLog;
            }
            set
            {
                _taskLog = value;
            }
        }

        string _dateFormat;
        string DateFormat
        {
            get
            {
                if(_dateFormat == null)
                {
                    var config = TaskSettings.GetConfig();
                    _dateFormat = config.DateFormat;
                }
                return _dateFormat;
            }
            set
            {
                _dateFormat = value;
            }
        }

        public TaskList _tasks;
        public TaskList TaskList
        {
            get
            {
                if(_tasks == null)
                {
                    var config = TaskSettings.GetConfig();
                    _tasks = config.TaskList;
                }
                return _tasks;
            }
            set
            {
                _tasks = value;
            }
        }

        public HandlerList _hndList;
        public HandlerList HandlerList
        {
            get
            {
                if(_hndList == null)
                {
                    var config = TaskSettings.GetConfig();
                    _hndList = config.HandlerList;
                }
                return _hndList;
            }
            set
            {
                _hndList = value;
            }
        }

        public TaskReader()
        {
        }
        public TaskReader(string logFormat)
        {
            _taskLog = logFormat;
        }

        public int Count
        {
            get
            {
                return TaskList.Count;
            }
        }
        public TaskBase Get(int index)
        {
            TaskBase retValue;
            if(_tasks.Count > index)
            {
                Task tsk = _tasks[index];
                string handler = tsk.Handler;
                retValue = TaskFactory.GetTaskHandler(handler);
                if(retValue == null)
                    throw new Exception("Could not find any handler with name " + handler + ".");
                retValue.Name = tsk.Name;
                retValue.Param = tsk.Param;
                return retValue;
            }
            return null;

        }
    }
}
